package com.goldmantis.alitrip.service;

import com.goldmantis.alitrip.common.BaseService;
import com.goldmantis.alitrip.common.sysenum.EnumButtonType;
import com.goldmantis.alitrip.common.SysTreeModel;
import com.goldmantis.alitrip.mapper.SysResMapper;
import com.goldmantis.alitrip.mapper.SysSubsystemMapper;
import com.goldmantis.alitrip.pojo.SysRes;
import com.goldmantis.alitrip.pojo.SysSubsystem;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Transformer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Author:Shmily
 * @Description:
 * @Date:Create in 9:46 2018/2/11
 */
@Service
public class SysResService extends BaseService<SysRes> {

    @Autowired
    private SysResMapper sysResMapper;

    @Autowired
    private SysSubsystemMapper sysSubsystemMapper;

    @Override
    public List<SysRes> select(SysRes t) {
        List<SysRes> sysReses = this.sysResMapper.getCustom(t);
        return sysReses;
    }

    public List<SysRes> getBySystemId(Long systemId) {
        Example example = new Example(SysRes.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("systemid", systemId);
        return this.sysResMapper.selectByExample(example);
    }

    public List<SysTreeModel> getSysTreeModel(Long systemId) {

        List<SysTreeModel> sysTreeModelList = new ArrayList<>();

        SysRes sysResCondition = new SysRes();
        sysResCondition.setSystemid(systemId);
        List<SysRes> sysReses = this.sysResMapper.getCustom(sysResCondition);

        //获取根目录
        for (SysRes sysRes : sysReses) {
            if (sysRes.getParentid().longValue() == 0) {
                SysTreeModel sysResModel = new SysTreeModel();
                sysResModel.setId(sysRes.getResid());
                sysResModel.setValue(sysRes.getResid());
                sysResModel.setLabel(sysRes.getResname());
                sysResModel.setOrder(sysRes.getSn());

                sysResModel.setFlag(false);
                sysResModel.setParentId(sysRes.getParentid());
                sysResModel.setAlias(sysRes.getAlias());
                sysResModel.setParentResName(sysRes.getParentResName());
                sysResModel.setSystemName(sysRes.getSysName());
                sysResModel.setDisplayInMenu(sysRes.getIsdisplayinmenu());
                sysResModel.setIsButton(sysRes.getIsButton());
                sysResModel.setSystemId(sysRes.getSystemid());
                sysTreeModelList.add(sysResModel);
            }
        }

        //递归获取子节点
        for (SysTreeModel sysResModel : sysTreeModelList) {
            sysResModel.setChildren(getChild(sysResModel.getId(), sysReses));
        }

        return sysTreeModelList;
    }


    public List<SysTreeModel> getSysTreeAllModel() {
        List<SysTreeModel> listSysTreeModel = new ArrayList<>();

        SysSubsystem sysSubsystem = new SysSubsystem();
        sysSubsystem.setParentid(1L);
        List<SysSubsystem> listSysSubsystem= this.sysSubsystemMapper.select(sysSubsystem);

        for (SysSubsystem sysSubsystem1: listSysSubsystem) {
            List<SysTreeModel> listtree= getSysTreeModel(sysSubsystem1.getSystemid());
            SysTreeModel sysTreeModel = new SysTreeModel();
            sysTreeModel.setId(sysSubsystem1.getSystemid());
            sysTreeModel.setValue(sysSubsystem1.getSystemid());
            sysTreeModel.setLabel(sysSubsystem1.getSysname());
            sysTreeModel.setChildren(listtree);
            listSysTreeModel.add(sysTreeModel);
        }
        return  listSysTreeModel;
    }


    private List<SysTreeModel> getChild(Long id, List<SysRes> allRess) {

        //获取子菜单
        List<SysTreeModel> sysTreeModelList = new ArrayList<>();
        for (SysRes sysRes : allRess) {
            if (sysRes.getParentid().longValue() == id.longValue()) {
                SysTreeModel sysResModel = new SysTreeModel();
                sysResModel.setId(sysRes.getResid());
                sysResModel.setValue(sysRes.getResid());
                sysResModel.setLabel(sysRes.getResname());
                sysResModel.setOrder(sysRes.getSn());

                sysResModel.setFlag(false);
                sysResModel.setParentId(sysRes.getParentid());
                sysResModel.setAlias(sysRes.getAlias());
                sysResModel.setParentResName(sysRes.getParentResName());
                sysResModel.setSystemName(sysRes.getSysName());
                sysResModel.setDisplayInMenu(sysRes.getIsdisplayinmenu());
                sysResModel.setIsButton(sysRes.getIsButton());
                sysResModel.setSystemId(sysRes.getSystemid());

                sysTreeModelList.add(sysResModel);
            }
        }

        //递归获取子节点
        for (SysTreeModel sysResModel : sysTreeModelList) {
            sysResModel.setChildren(getChild(sysResModel.getId(), allRess));
        }

        if (sysTreeModelList.size() == 0) {
            return null;
        }
        return sysTreeModelList;

    }

    @Override
    public SysRes selectById(Long id) {

        SysRes sysRes = super.selectById(id);

        if (sysRes.getIsButton() != null) {
            SysRes item = new SysRes();
            item.setParentid(sysRes.getResid());
            List<SysRes> buttons = this.sysResMapper.select(item);

            List<Integer> buttonTypes = new ArrayList<>();
            for (SysRes button : buttons) {
                if(button.getButtonType()!=null) {
                    buttonTypes.add(button.getButtonType().intValue());
                }
            }
            sysRes.setButtons(buttonTypes);
        }

        if (!StringUtils.isEmpty(sysRes.getPath())) {
            String[] pathes = StringUtils.split(sysRes.getPath(), ':');
            List<String> pathStrings = Arrays.asList(pathes);

            List<Long> pathLongs = new ArrayList<>();


            //String List 与Long List的转换
            CollectionUtils.collect(pathStrings, new Transformer<String, Long>() {
                @Override
                public Long transform(String input) {
                    return Long.valueOf(input);
                }
            }, pathLongs);


            //删除掉本级路径
            pathLongs.remove(pathLongs.size() - 1);
            sysRes.setPathList(pathLongs);
        }

        return sysRes;

    }

    public void insertCustom(SysRes sysRes) {
        if (sysRes.getParentid() == null) {
            sysRes.setParentid(0L);
        }
        sysRes.setButtonType((short)1);
        sysRes.setIsopen((short)0);
        super.insert(sysRes);
        List<Long> tempParent = new ArrayList<Long>();
        tempParent.add(sysRes.getResid());
        List<Long> pathParent = getPath(sysRes, tempParent);
        Collections.reverse(pathParent);
        sysRes.setPath(StringUtils.join(pathParent, ':'));
        super.update(sysRes);


        if (!CollectionUtils.isEmpty(sysRes.getButtons())) {
            for (Integer id : sysRes.getButtons()) {

                SysRes item = new SysRes();
                item.setResname(sysRes.getResname() + EnumButtonType.getTextByValue(id));
                item.setSn(id.longValue());
                item.setParentid(sysRes.getResid());
                item.setSystemid(sysRes.getSystemid());
                item.setIsButton((short) 1);
                item.setButtonType(id.shortValue());
                super.insert(item);

                List<Long> temp = new ArrayList<Long>();
                temp.add(item.getResid());
                List<Long> path = getPath(item, temp);
                Collections.reverse(path);
                item.setPath(StringUtils.join(path, ':'));
                super.update(item);

            }
        }
    }

    public void updateCustom(SysRes sysRes) {
        super.update(sysRes);

        List<Long> tempParent = new ArrayList<Long>();
        tempParent.add(sysRes.getResid());
        List<Long> pathParent = getPath(sysRes, tempParent);
        Collections.reverse(pathParent);
        sysRes.setPath(StringUtils.join(pathParent, ':'));
        super.update(sysRes);

        //找到原先的按钮 全部删除掉
        SysRes itemOri = new SysRes();
        itemOri.setParentid(sysRes.getResid());
        List<SysRes> buttonsOri = this.sysResMapper.select(itemOri);
        for (SysRes item : buttonsOri) {
            final int delete = this.sysResMapper.delete(item);
        }

        if (!CollectionUtils.isEmpty(sysRes.getButtons())) {
            for (Integer id : sysRes.getButtons()) {

                SysRes item = new SysRes();
                item.setResname(sysRes.getResname() + EnumButtonType.getTextByValue(id));
                item.setSn(id.longValue());
                item.setParentid(sysRes.getResid());
                item.setSystemid(sysRes.getSystemid());
                item.setIsButton((short) 1);
                item.setButtonType(id.shortValue());
                super.insert(item);

                List<Long> temp = new ArrayList<Long>();
                temp.add(item.getResid());
                List<Long> path = getPath(item, temp);
                Collections.reverse(path);
                item.setPath(StringUtils.join(path, ':'));
                super.update(item);

            }
        }
    }


    public List<Long> getPath(SysRes sysRes, List<Long> temp) {
        if (sysRes.getParentid() != null && sysRes.getParentid().longValue() != 0) {
            SysRes item = new SysRes();
            item.setResid(sysRes.getParentid());
            SysRes res = this.sysResMapper.select(item).get(0);
            temp.add(res.getResid());
            getPath(res, temp);
        }
        return temp;
    }
}
